package com.qianfeng.gl4study.youshare.tasks;

import android.os.AsyncTask;
import com.qianfeng.gl4study.youshare.utils.HttpTool;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/5
 * Email:pdsfgl@live.com
 */
public class RegisterTask extends AsyncTask<String,Integer,JSONObject> {


	//用于调用实际接收数据的对象，可以认为是Activity/Fragment.来进行UI的更新
	private TaskProcessor processor;

	public RegisterTask(TaskProcessor processor){
		this.processor = processor;
	}

	@Override
	protected JSONObject doInBackground(String... params) {
		JSONObject ret = null;

		if(params!=null&&params.length>=3) {
			String userName = params[0];
			String userPassword1 = params[1];
			String userPassword2 = params[2];
			String mobileCode = params[3];

			String url = "http://open.doushangshang.com/user.php?ac=register";
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("user",userName);
			map.put("password",userPassword1);
			map.put("conform_password",userPassword2);
			map.put("mobile_code",mobileCode);

			byte[] bytes = HttpTool.post(url, map);
			try {
				String string = new String(bytes, "UTF-8");
				ret = new JSONObject(string);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	@Override
	protected void onPostExecute(JSONObject jsonObject) {
		if(processor!=null){
			processor.processResult(jsonObject,2);
		}
	}
}
